import OSS from "ali-oss";

const client = new OSS.Wrapper({
    region: "oss-cn-hangzhou",
    accessKeyId: "LTAI5tFS1ACfWCnwrLhHkPB8",
    accessKeySecret: "Ec9OmNiZrFlFpQlLvpML3OIxyFFVlo",
    bucket: "ai-assistant-client",
    secure: true,
});

export async function downloadOssFile(args) {
    let {path, type} = args;
    try {
        let res = await client.get(path);
        return {blob: res.content, path: path};
    } catch (e) {
        if (e.code === "NoSuchKey" && type === "count") {
            return {blob: null, path: path};
        } else {
            throw e;
        }
    }
}

export async function uploadOssFile(args) {
    let {fileName, fileBuffer, type, user} = args;
    let path = `${user}/${type}/${fileName}`;

    client.put(path, new Buffer.from(fileBuffer));
}